<?php
require_once DIR_DEFS.'error.php';

define ('DATE_STRLEN', 10);

/**
 * Description of CommonClass
 * 
 * used for common error handling and debugging.
 *
 * @author GHoogendoorn
 * @version 0.1
 * 
 * Version history
 * 0.1  GHoogendoorn    Initial version
 * 
 */
class CommonClass extends Error{

    private $error_class;
    
    public function __construct() {
        // As of PHP 5.3.3 CommonClass() treated as regular method
        $this->error_class = new Error();
        
    }
    
    public function setError( $error_txt ){
        $error = new Error();
        $error->setError( $error_txt );

    }
    
    public function setErrorArray($error_array){
        $error = new Error();
        $error->setErrorArray($error_array);
        // $this->error_class->setErrorArray($error_array);
    } 
    
    public function getErrorArray(){
        $error = new Error();
        return $error->getErrorArray();
    }
    
    public function getFirsthError(){
        $error = new Error();
        return $error->getFirsthError();
    }


    public function getErrorNr(){
        $error = new Error();
        return $error->getErrorNr();
    }
    
    protected function reset(){
        $error = new Error();
        return $error->reset();
    }
    
    protected function dumpStringCharCodes($string){
        for ( $i=0; $i<strlen($string); $i++ ){
            echo '['.ord(substr($string, $i, 1)).']';
        }
        echo "<br />\n";
    }
    
    /**
     *
     * checkDate()
     * Check the date formatted field
     * 
     * @param String $date (FormatYYYY-MM-DD)
     * @param Bool $empty if date may be empty set to TRUE
     *
     */
    protected function checkDate( $date, $empty = FALSE){
        
        if ( ($empty == FALSE ) && empty($date) ){
            throw new Exception(TXT_ERROR_EMPTY_VAR.'['. TXT_DATE.']');
            
        } else if (empty($date)){
            /** Dont check **/
            return;
        }
        
        
        if (strlen(trim($date)) !== DATE_STRLEN ){
            
            throw new Exception(TXT_ERROR_INVALID_DATE_LEN);
        }
        
        $date_array = explode('-', $date);
        if( 
            ( $date_array == FALSE )            ||  // No delimiter found  
            ( strlen($date_array[0]) !== 4 )    ||  //year
            ( strlen($date_array[1]) !== 2 )    ||  //month
            ( strlen($date_array[2]) !== 2  )       //day
        ){
            throw new Exception(TXT_ERROR_INVALID_DATE_FORMAT . $date);
        } 
    }

}

class TestCommonClass {

    public function TestCommonClass() {
        $test = new CommonClass();
    }

}
?>
